package com.ruoyi.rsikManage.Alarm.controller;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.rsikManage.Alarm.domain.PersonCardInfo;
import com.ruoyi.rsikManage.Alarm.domain.TBaseAlarmsRequest;
import com.ruoyi.rsikManage.Alarm.domain.vo.AlarmEFRequest;
import com.ruoyi.rsikManage.Alarm.service.ITBaseAlarmsService;
import com.ruoyi.rsikManage.MonitorPoint.domain.TMonitorPointRequest;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.rsikManage.Alarm.domain.TBaseAlarms;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报警信息Controller
 *
 * @author sxygsj
 * @date 2024-12-26
 */
@RestController
@RequestMapping("/riskManage/base/alarms")
public class TBaseAlarmsController extends BaseController
{


    private final ITBaseAlarmsService tAlarmsService;
    private final TokenService tokenService;

    public TBaseAlarmsController(ITBaseAlarmsService tAlarmsService,
                                 TokenService tokenService) {
        this.tAlarmsService = tAlarmsService;
        this.tokenService = tokenService;
    }
    /**
     * 查询报警信息列表
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:alarms:list')")
    @GetMapping("/list")
    public TableDataInfo list(TBaseAlarms tBaseAlarms)
    {
        startPage();
        List<TBaseAlarms> list = tAlarmsService.selectTAlarmsList(tBaseAlarms);
        return getDataTable(list);
    }

    /**
     * 导出报警信息列表
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:alarms:export')")
    @Log(title = "报警信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TBaseAlarms tBaseAlarms)
    {
        List<TBaseAlarms> list = tAlarmsService.selectTAlarmsList(tBaseAlarms);
        ExcelUtil<TBaseAlarms> util = new ExcelUtil<TBaseAlarms>(TBaseAlarms.class);
        util.exportExcel(response, list, "报警信息数据");
    }

    /**
     * 获取报警信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:alarms:query')")
    @GetMapping(value = "/{idId}")
    public AjaxResult getInfo(@PathVariable("idId") Long idId)
    {
        return success(tAlarmsService.selectTAlarmsByIdId(idId));
    }

    /**
     * 新增报警信息
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:alarms:add')")
    @Log(title = "报警信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBaseAlarms tBaseAlarms)
    {
        return toAjax(tAlarmsService.insertTAlarms(tBaseAlarms));
    }

    /**
     * 修改报警信息
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:alarms:edit')")
    @Log(title = "报警信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBaseAlarms tBaseAlarms)
    {
        return toAjax(tAlarmsService.updateTAlarms(tBaseAlarms));
    }

    /**
     * 删除报警信息
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:alarms:remove')")
    @Log(title = "报警信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{idIds}")
    public AjaxResult remove(@PathVariable Long[] idIds)
    {
        return toAjax(tAlarmsService.deleteTAlarmsByIdIds(idIds));
    }




    /**
     * 统计通道离线记录列表 年月日 类型
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:monitorOffline:list')")
    @RequestMapping(value = "/count", method = {RequestMethod.GET, RequestMethod.POST})
    public TableDataInfo hour(HttpServletRequest httpServletRequest,
                              @RequestBody(required = false) TBaseAlarmsRequest tBaseAlarmsRequest)
    {

        // 初始化单位代码列表
        List<Long> unitCodes = new ArrayList<>();

        // 如果请求体不为空，则从请求体中获取单位代码
        if (tBaseAlarmsRequest != null) {
            unitCodes = tBaseAlarmsRequest.getUnitCodes();
        }

        // 如果单位代码列表为空，则尝试从当前登录用户获取单位代码
        if (CollectionUtils.isEmpty(unitCodes)) {
            if (httpServletRequest != null && tokenService.getTokenInfo(httpServletRequest) != null) {
                unitCodes = tokenService.getLoginUserUnitCodes(httpServletRequest);
            }
        }

        // 设置最终确定的单位代码到请求参数中
        if (tBaseAlarmsRequest == null) {
            tBaseAlarmsRequest = new TBaseAlarmsRequest();
        }
        tBaseAlarmsRequest.setUnitCodes(unitCodes);
        List<TBaseAlarms> tBaseAlarms = tAlarmsService.selectTAlarmsCount(tBaseAlarmsRequest);
        List<Map<String,Object>> list = new ArrayList<>();
        String countType = tBaseAlarmsRequest.getCountType();


        return getDataTable(tBaseAlarms);
    }


    /**
     * 统计通道离线记录列表 年月日 类型
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:monitorOffline:list')")
    @RequestMapping(value = "/receive-alarm-data", method = {RequestMethod.GET, RequestMethod.POST})
    public AlarmEFRequest receive(HttpServletRequest httpServletRequest,
                              @RequestBody(required = false) TBaseAlarmsRequest tBaseAlarmsRequest) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yy-HH-dd HH:mm:ss");
        String format = now.format(dateTimeFormatter);

        int second = LocalDateTime.now().getSecond();
        String a = null;
        AlarmEFRequest alarmEFRequest = new AlarmEFRequest();
        alarmEFRequest.setMsg("成功");
        alarmEFRequest.setResult("Succeed");
        alarmEFRequest.setAlarmTime(format);
        List<String> arrayList = new ArrayList<>();
        PersonCardInfo personCardInfo = new PersonCardInfo();

        if (second<10){

            alarmEFRequest.setApeIP("");
            alarmEFRequest.setApeName("");
            alarmEFRequest.setControllerIP("192.168.14.27");
            alarmEFRequest.setControllerStatus("0");
            alarmEFRequest.setPictureUrl(format);
            arrayList.add("1");
            alarmEFRequest.setAlarmType(arrayList);
            personCardInfo.setAlarmList(arrayList);
            personCardInfo.setShutdownList(arrayList);
            alarmEFRequest.setPersonCardInfo(personCardInfo);

        }

        else if (second<20){

            alarmEFRequest.setApeIP("");
            alarmEFRequest.setApeName("");
            alarmEFRequest.setControllerIP("192.168.14.27");
            alarmEFRequest.setControllerStatus("2");
            alarmEFRequest.setPictureUrl(format);
            arrayList.add("1");
            alarmEFRequest.setAlarmType(arrayList);
            personCardInfo.setAlarmList(arrayList);
            personCardInfo.setShutdownList(arrayList);
            alarmEFRequest.setPersonCardInfo(personCardInfo);

        }else if(second<40){
            personCardInfo.setAlarmList(arrayList);
            personCardInfo.setShutdownList(arrayList);
            alarmEFRequest.setApeIP("");
            alarmEFRequest.setApeName("b");
            alarmEFRequest.setControllerIP("192.168.14.27");
            alarmEFRequest.setControllerStatus("1");
            alarmEFRequest.setPictureUrl("a"+format);
            arrayList.add("1");
            alarmEFRequest.setAlarmType(arrayList);
            personCardInfo.setAlarmList(arrayList);
            personCardInfo.setShutdownList(arrayList);
            alarmEFRequest.setPersonCardInfo(personCardInfo);


        }else if(second<60){
            personCardInfo.setAlarmList(arrayList);
            personCardInfo.setShutdownList(arrayList);
            alarmEFRequest.setApeIP("");
            alarmEFRequest.setApeName("b");
            alarmEFRequest.setControllerIP("192.168.14.27");
            alarmEFRequest.setControllerStatus("2");
            alarmEFRequest.setPictureUrl("b"+format);
            arrayList.add("2");
            alarmEFRequest.setAlarmType(arrayList);
            personCardInfo.setAlarmList(arrayList);
            personCardInfo.setShutdownList(arrayList);
            alarmEFRequest.setPersonCardInfo(personCardInfo);

        }

        return alarmEFRequest;
    }

}
